package com.weilan.ark3.controller;

import com.weilan.ark3.buffer.MsgQueue;
import com.weilan.ark3.common.GlobalParam;
import com.weilan.ark3.entity.ControlDto;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("weilan/system/mode")
public class ModeController {

    @PostMapping("/change")
    public void handoffMode(@RequestParam Integer mode) {
        ControlDto controlDto1 = new ControlDto();
        if (mode != null) {
            switch (mode) {
                //并网
                case 1:
                    controlDto1.setSlaveId(GlobalParam.guangbo);
                    controlDto1.setAddress(300);
                    controlDto1.setValue(1);
                    MsgQueue.writeToControlTcp(controlDto1);
                    GlobalParam.fgState = 0;
                    break;
                //离网
                case 2:
                    GlobalParam.fgState = 0;
                    controlDto1.setSlaveId(GlobalParam.guangbo);
                    controlDto1.setAddress(300);
                    controlDto1.setValue(2);
                    MsgQueue.writeToControlTcp(controlDto1);
                    break;
                //并离网
                case 3:
                    controlDto1.setSlaveId(GlobalParam.guangbo);
                    controlDto1.setAddress(300);
                    controlDto1.setValue(3);
                    MsgQueue.writeToControlTcp(controlDto1);
                    break;
                //峰谷
                case 4:
                    GlobalParam.fgState = 1;
                    break;
                //备电
                case 5:
                    GlobalParam.backState = 1;
                    break;
            }
        }
    }

    //启停控制
    @PostMapping("/switch")
    public void onoff(@RequestParam Integer state) {
        ControlDto controlDto = new ControlDto();
        if (state != null) {
            switch (state) {
                case 2:
                    controlDto.setSlaveId(GlobalParam.guangbo);
                    controlDto.setAddress(301);
                    controlDto.setValue(2);
                    MsgQueue.writeToControlTcp(controlDto);
                    break;
                case 6:
                    controlDto.setSlaveId(GlobalParam.guangbo);
                    controlDto.setAddress(301);
                    controlDto.setValue(6);
                    MsgQueue.writeToControlTcp(controlDto);
                    break;
            }

        }
    }

    //故障清除
    @PostMapping("/clean")
    public void failclean(@RequestParam Integer clean) {
        ControlDto controlDto = new ControlDto();
        if (clean != null) {
            switch (clean) {
                case 0:
                    controlDto.setSlaveId(255);
                    controlDto.setAddress(328);
                    controlDto.setValue(0);
                    MsgQueue.writeToControlTcp(controlDto);
                    break;
                case 1:

                    controlDto.setSlaveId(255);
                    controlDto.setAddress(328);
                    controlDto.setValue(1);
                    MsgQueue.writeToControlTcp(controlDto);
                    break;
            }
        }
    }

    /*
     * 功率下发
     * */
    @PostMapping("/power")
    public static void sendPower(@RequestParam Integer power) {
        if (power != null) {
            //系统故障和高低压保护
        }
    }

    /*
    * 控制权转换
    * */
    @PostMapping("/ctlpower")
    public static void ctlpower(@RequestParam Integer ctl){
        ControlDto controlDto = new ControlDto();
        if (ctl != null){
            // 0:低速控制（EMS）
            if (ctl == 0){
                controlDto.setSlaveId(255);
                controlDto.setAddress(331);
                controlDto.setValue(0);
                MsgQueue.writeToControlTcp(controlDto);
                //高速控制-客户
            }else if (ctl == 1){
                controlDto.setSlaveId(255);
                controlDto.setAddress(331);
                controlDto.setValue(1);
                MsgQueue.writeToControlTcp(controlDto);
            }
        }
    }
}
